from tkinter import *
# 导入ttk
from tkinter import ttk
# 导入filedialog
from tkinter import filedialog
from tkinter.messagebox import *
from tkinter import *
import requests
import re
import pyttsx3
import _thread


base_url = "https://www.suibkk.com/read/"
init_boot='351509'
init_page='76455836'

books = [
    {"book": "351509", "page": "76455819", "name": "大明锦衣卫：惩奸除恶奖励万物"},
    {"book": "616287", "page": "91774896", "name": "三斤糙米"},
    {"book": "351509", "page": "76455819", "name": "大明锦衣卫：惩奸除恶奖励万物"},
    {"book": "351509", "page": "76455819", "name": "大明锦衣卫：惩奸除恶奖励万物"},
    {"book": "351509", "page": "76455819", "name": "大明锦衣卫：惩奸除恶奖励万物"}

]


class App:

    def __init__(self, root):
        self.root = root
        self.read = 1
        self.is_open_type = False  # False表示当前未开启，True表示当前开启
        self.initWidgets()

    def initWidgets(self):
        self.get_label1 = Label(self.root, text='book')
        self.get_text1 = Text(self.root, height=1, width=10)
        self.get_text1.insert('0.0', init_boot)

        self.get_label2 = Label(self.root, text='page')
        self.get_text2 = Text(self.root, height=1, width=10)
        self.get_text2.insert('0.0', init_page)

        self.button_get = Button(self.root, text='start', command=self.text_get)
        self.button_get2 = Button(self.root, text='end', command=self.text_get2)

        self.get_label1.grid(row=1, column=0, sticky=E)
        self.get_text1.grid(row=1, column=1, sticky=W)
        self.get_label2.grid(row=2, column=0, sticky=E)
        self.get_text2.grid(row=2, column=1, sticky=W)

        self.button_get.grid(row=3, column=0, sticky=W)
        self.button_get2.grid(row=3, column=1, sticky=W)

    def text_get(self):
        self.read=1
        print("===========start read============")
        _thread.start_new_thread(self.get_msg,())
    def text_get2(self):
        self.read=0
        print("===========stop read============")
    def get_msg(self):
        while self.read:
            book=str(self.get_text1.get('0.0', END)).replace("\n","")
            page=str(self.get_text2.get('0.0', END)).replace("\n","")
            full_url = base_url + book + "/" + page
            print(full_url)
            bbb = 10
            print_if = 0
            html = requests.get(full_url)
            html.encoding = 'utf-8'
            # html.encoding = 'gbk'
            pattern = re.compile(r'[^\u4e00-\u9fa5]')
            chinese = re.sub(pattern, "", html.text)
            # print(html.text)
            xxxxx = html.text.replace("<br><br>", "\n")
            xxxxx = xxxxx.replace("<br /><br />", "\n")
            xxxxx = xxxxx.replace("<br/><br/>", "\n")
            xxxxx = xxxxx.replace("</p><p>", "\n")
            xxxxx = xxxxx.replace("<p>", "\n")
            xxxxx = xxxxx.replace("<br />　　<br />", "\n")
            xxxxx = xxxxx.replace("<br />", "")
            xxxxx = xxxxx.replace("“", "")
            xxxxx = xxxxx.replace("”", "")
            xxxxx = xxxxx.replace("&nbsp;&nbsp;&nbsp;&nbsp;", "\t")
            # print(xxxxx)
            # start = xxxxx.find("id=\"content\"")
            # end = xxxxx.find("nofollow")
            start = xxxxx.find("nr_nr")
            end = xxxxx.find("pb_next")
            zzzz = xxxxx[start + 25:end - 646]
            zzzz = zzzz.replace("<", "")
            zzzz = zzzz.replace("", "")
            if print_if:
                print(zzzz)
            xdf = 0
            try:
                xdf = html.text.index(">下一页")
            except:
                try:
                    xdf = html.text.index(">下一章")
                except:
                    print("next")
            print(html.text[xdf - 50:xdf])
            next_papge = html.text[xdf - 50:xdf].replace("\"", "").split("/read/" + book + "/")[1]
            print(next_papge)
            self.get_text2.delete('0.0', END)
            self.get_text2.insert('0.0', next_papge)
            if bbb:
                self.say(zzzz)

    def say(self,str):
        engine = pyttsx3.init()
        engine.setProperty('rate', 400)
        engine.say(str)
        engine.runAndWait()


root = Tk()
root.title('.')
root.geometry("100x80")
App(root)


root.mainloop()